
use lazy_static::lazy_static;
use rusqlite::{Connection, Error};
use std::sync::Mutex;

lazy_static! {
    pub static ref DB_CONNECTION: Mutex<Connection> = Mutex::new(
        Connection::open("data.db").expect("Failed to create database connection")
    );
}


pub fn init() -> Result<(), Error> {
    let conn = DB_CONNECTION.lock().unwrap();
    let sql = r#"
    CREATE TABLE IF NOT EXISTS OrderDetails (
        OrderID INTEGER PRIMARY KEY AUTOINCREMENT,
        Code VARCHAR(20),
        CustomerName VARCHAR(255) NOT NULL,
        PhoneNumber VARCHAR(20),
        Address TEXT,
        ProductName VARCHAR(255) NOT NULL,
        Color VARCHAR(50),
        Unit VARCHAR(20),
        Width DECIMAL(10, 2),
        Length DECIMAL(10, 2),
        Quantity INT,
        Area DECIMAL(10, 3),
        UnitPrice DECIMAL(10, 2),
        DiscountRate DECIMAL(5, 2),
        Amount DECIMAL(10, 2),
        Note TEXT,
        CreatedBy VARCHAR(100),
        CreatedDate DATE
    );"#;

    conn.execute(sql, [])?;
    
    Ok(())
}